﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Globalization;

namespace VPLink2013.Framework.Extensions
{
    public static partial class FrameworkExtensions
    {
        /// <summary>
        /// Formatted date according to input culture parameter
        /// </summary>
        /// <param name="input">DateTime object</param>
        /// <param name="culture">String representing a culture. For French should be "fr-FR". If culture is null or empty the culture is taken from current thread culture</param>
        /// <returns></returns>
        public static string ToSmallDate(this DateTime input, string culture )
        {
            try
            {
                if (culture.IsNullOrEmptyOrWhitespace())
	            {
		            return input.ToShortDateString();
	            }

                //backup current culture
                CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;

                Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
                string result = input.ToShortDateString();

                // Restore original culture.
                Thread.CurrentThread.CurrentCulture = originalCulture;

                return result;
                
            }
            catch (Exception ex)
            {
                //TODO : log
                return string.Empty;
            }
        }


        /// <summary>
        /// Formatted date according to input culture parameter
        /// </summary>
        /// <param name="input">DateTime object</param>
        /// <param name="culture">String representing a culture. For French should be "fr-FR". If culture is null or empty the culture is taken from current thread culture</param>
        /// <returns></returns>
        public static string ToSmallDate(this DateTime? input, string culture)
        {
            try
            {
                if (input == null)
                {
                    return string.Empty;
                }

                return input.Value.ToSmallDate(culture);

            }
            catch (Exception ex)
            {
                //TODO : log
                return string.Empty;
            }
        }


        /// <summary>
        /// Change the time value to current time inside input date. 
        /// The date part of the input paramaeter (year/month/day) is not modified.
        /// </summary>
        /// <param name="input">A nullable DateTime value</param>
        /// <returns></returns>
        public static DateTime? InjectCurrentTime(this DateTime? input)
        {
            try
            {
                if (input == null)
                {
                    return null;
                }

                DateTime currentDateTime = System.DateTime.Now;
                DateTime inputDate = input.Value;
                DateTime inputDateWithCurrentTime = new DateTime(
                                inputDate.Year, inputDate.Month, inputDate.Day,
                                currentDateTime.Hour, currentDateTime.Minute, currentDateTime.Second);

                return inputDateWithCurrentTime;

            }
            catch (Exception ex)
            {
                //TODO : log
                return input;
            }
        }


    }
}
